package cn.jiangjiesheng.bootstrap.fileport.infrastructure.utils;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by dev@jiangjiesheng.cn on 2021/3/17  <br/>
 */
public class FileUtils {
    /**
     * 读取数据
     *
     * @param resourceName
     * @return
     */
    public static String readStringFromResource(String resourceName) {
        URL url = Resources.getResource(resourceName);
        try {
            return Resources.toString(url, Charsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static InputStream readInputStreamFromResource(String resourceName) {
        URL url = Resources.getResource(resourceName);
        try {
            return new ByteArrayInputStream(Resources.toByteArray(url));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
